package ${cfg.parent}.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xc.core.bean.QueryBean;
import com.xc.core.utils.ObjectUtil;

/**
 * Service层封装数据
 */
public class ServiceUtils {
    /**
     * 封装查询
     *
     * @param queryBean 查询排序条件
     * @param t         用户查询实体
     * @return 返回QueryWrapper查询对象
     */
    public static <T> QueryWrapper<T> queryData(QueryBean queryBean, T t) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
        boolean bool = false;
        if ("ASC".toUpperCase().equals(queryBean.getRule().toUpperCase())) {
            bool = true;
        }
        if (queryBean.getSortFields() == null) {
            queryWrapper.orderBy(true, bool, "id");
        } else {
            queryWrapper.orderBy(true, bool, queryBean.getSortFields());
        }
        if (queryBean.getLikeFields() != null) {
            for (String likeField : queryBean.getLikeFields()) {
                queryWrapper.like(likeField, ObjectUtil.getValueByField(t, likeField));
            }
        }
        queryWrapper.setEntity(t);
        return queryWrapper;
    }
}
